package echat.future.cn.echat.my.adapter;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.text.SimpleDateFormat;
import java.util.List;

import echat.future.cn.echat.R;
import echat.future.cn.echat.my.bean.ExpenBean;
import echat.future.cn.echat.my.utils.ImageLoadUtils;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.view.CapitalActivity;

public class CapitalAdapter extends BaseQuickAdapter<ExpenBean,BaseViewHolder> {

    private int type;

    public CapitalAdapter(@Nullable List<ExpenBean> data) {
        super(R.layout.item_capital,data);
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    protected void convert(BaseViewHolder helper, ExpenBean item) {
        ImageLoadUtils.loadImage(mContext, UserHelper.getUser().getAvatar(),helper.getView(R.id.circleImageView));
        helper.setText(R.id.nickName,UserHelper.getUser().getUsername()+"")
                .setText(R.id.level,"LV."+ (UserHelper.getUser().getLevel() == null ?0:"1"))
                .setText(R.id.time,"时间："+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item.getCreate_time()*1000));

        SpannableString spannableString = null;
        ForegroundColorSpan spannable = null;
        if(type == CapitalActivity.TYPE_EXOEBDITURE){
            //支出 1为一对一视频消费（视频聊天消费），2为打赏，3为付费小视频，4为提现
            spannable = new ForegroundColorSpan(Color.parseColor("#F6345E"));
            if(item.getType() == 1){
                spannableString = new SpannableString("消费支出："+item.getE_money());
                helper.setText(R.id.contanct_time,"通话时长："+item.getChat_time()+"分钟");
            }else if(item.getType() == 2){
                spannableString = new SpannableString("打赏支出："+item.getE_money());
                helper.setText(R.id.contanct_time,"送礼");
            }else if(item.getType() == 3){
                spannableString = new SpannableString("小视频支出："+item.getE_money());
                helper.setText(R.id.contanct_time,"小视频");
            }else{
                spannableString = new SpannableString("提现支出："+item.getE_money());
                helper.setText(R.id.contanct_time,"提现");
            }
        }

        if(type ==  CapitalActivity.TYPE_INCOME){
            //收入
            spannable = new ForegroundColorSpan(Color.parseColor("#2EBE50"));
            /**
             * E币来源：【充值：1，平台调整：2，视频消费：3，打赏：4，下级消费奖励：6，周周榜奖励：7 】
             */
            if(item.getType() == 1){
                spannableString = new SpannableString("充值收入："+item.getE_money());
                helper.setText(R.id.contanct_time,"充值");
            }else if(item.getType() == 2){
                spannableString = new SpannableString("直播收入："+item.getE_money());
                helper.setText(R.id.contanct_time,"通话时长："+(item.getChat_time() == null?"0秒":formatDateTime(Long.parseLong(item.getChat_time()))));
            }else if(item.getType() == 3){
                spannableString = new SpannableString("打赏收入："+item.getE_money());
                helper.setText(R.id.contanct_time,"送礼");
            }else if(item.getType() == 4){
                spannableString = new SpannableString("下级奖励："+item.getE_money());
                helper.setText(R.id.contanct_time,"下级消费奖励");
            }else{
                spannableString = new SpannableString("周榜奖励："+item.getE_money());
                helper.setText(R.id.contanct_time,"周周榜奖励");
            }
        }
        spannableString.setSpan(spannable,5,spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        helper.setText(R.id.emoney,spannableString);

        if(type == CapitalActivity.TYPE_INCOME){
            helper.setGone(R.id.level,true);
        }else{
            helper.setGone(R.id.level,false);
        }
    }


    public static String formatDateTime(long mss) {
        String DateTimes = null;
        long days = mss / ( 60 * 60 * 24);
        long hours = (mss % ( 60 * 60 * 24)) / (60 * 60);
        long minutes = (mss % ( 60 * 60)) /60;
        long seconds = mss % 60;
        if(days>0){
            DateTimes= days + "天" + hours + "小时" + minutes + "分"
                    + seconds + "秒";
        }else if(hours>0){
            DateTimes=hours + "小时" + minutes + "分"
                    + seconds + "秒";
        }else if(minutes>0){
            DateTimes=minutes + "分"
                    + seconds + "秒";
        }else{
            DateTimes=seconds + "秒";
        }
        return DateTimes;
    }
}
